package cn.zyjblogs.nio;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel02 {
    public static void main(String[] args) {
        //创建文件的输入流
        File file  = new File("d:\\file01.txt");
        try(FileInputStream fileInputStream = new FileInputStream(file)) {
            //通过fileInputStream 获取对应的FileChannel 这个FileChannel 正是类型使FileChannelImpl
            FileChannel fileChannel = fileInputStream.getChannel();

            //创建一个缓冲区ByteBuffer
            ByteBuffer buffer = ByteBuffer.allocate((int) file.length());

            //将fileChannel通道的数据读入到buffer
            fileChannel.read(buffer);
            //ByteBuffer 的字节数据 转成string
            System.out.println(new String(buffer.array()));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
